From 87033855500e2c39279c74f43445abfd32787e41 Mon Sep 17 00:00:00 2001 From: tsteven4 Date: Fri, 30 Aug 2013 21:25:48 +0000 Subject: [PATCH] fix some url related memory leaks. add constructor for UrlLink from char*. --- gpsbabel/an1.cc | 6 ++---- gpsbabel/cst.cc | 7 +++++-- gpsbabel/defs.h | 7 +++++-- gpsbabel/easygps.cc | 39 +++++++++++++++++++++++++++------------ gpsbabel/garmin_gpi.cc | 3 +++ gpsbabel/garmin_txt.cc | 3 +-- gpsbabel/gdb.cc | 6 +++--- gpsbabel/mmo.cc | 11 +++++------ gpsbabel/shape.cc | 3 +-- gpsbabel/tmpro.cc | 3 +-- gpsbabel/tpo.cc | 6 ++---- gpsbabel/unicsv.cc | 3 +-- 12 files changed, 56 insertions(+), 41 deletions(-) diff --git a/gpsbabel/an1.cc b/gpsbabel/an1.cc index 11a6e9f78..954d081e2 100644 --- a/gpsbabel/an1.cc +++ b/gpsbabel/an1.cc @@ -710,14 +710,12 @@ static void Read_AN1_Waypoints(gbfile* f) wpt_tmp->notes = xstrdup(rec->comment); wpt_tmp->description = xstrdup(rec->name); if (rec->url) { - UrlLink l(rec->url); - wpt_tmp->AddUrlLink(l); + wpt_tmp->AddUrlLink(rec->url); } else if (NULL != (url=strstr(wpt_tmp->description, "{URL="))) { *url = '\0'; url += 5; url[strlen(url)-1] = '\0'; - UrlLink l(url); - wpt_tmp->AddUrlLink(l); + wpt_tmp->AddUrlLink(url); } if (rec->image_name) { diff --git a/gpsbabel/cst.cc b/gpsbabel/cst.cc index dec667de3..b2b2d8f45 100644 --- a/gpsbabel/cst.cc +++ b/gpsbabel/cst.cc @@ -216,8 +216,11 @@ cst_data_read(void) if (strncmp(cin + 2, "bitmap", 6) == 0) { cin = lrtrim(cin + 8); if (*cin != '\0') { - UrlLink l(cst_make_url(cin)); - wpt->AddUrlLink(l); + char* url = cst_make_url(cin); + wpt->AddUrlLink(url); + if (url) { + xfree(url); + } } } diff --git a/gpsbabel/defs.h b/gpsbabel/defs.h index 9cfae14d9..60a841aa2 100644 --- a/gpsbabel/defs.h +++ b/gpsbabel/defs.h @@ -369,10 +369,13 @@ class UrlLink { public: UrlLink() { } - UrlLink(QString url) : + UrlLink(const QString url) : url_(url) { } - UrlLink(QString url, QString url_link_text) : + UrlLink(const char* url) : + url_(url) + { } + UrlLink(const QString url, const QString url_link_text) : url_(url), url_link_text_(url_link_text) { } diff --git a/gpsbabel/easygps.cc b/gpsbabel/easygps.cc index 37759fadf..1a25c67a4 100644 --- a/gpsbabel/easygps.cc +++ b/gpsbabel/easygps.cc @@ -93,26 +93,41 @@ data_read(void) break; case 2: case 3: - wpt_tmp->description = gbfgetpstr(file_in);; + wpt_tmp->description = gbfgetpstr(file_in); break; case 5: - wpt_tmp->notes = gbfgetpstr(file_in);; - break; - case 6: - link.url_link_text_ = gbfgetpstr(file_in);; + wpt_tmp->notes = gbfgetpstr(file_in); break; + case 6: { + char* ult = gbfgetpstr(file_in); + link.url_link_text_ = ult; + if (ult) { + xfree(ult); + } + } + break; case 7: - wpt_tmp->icon_descr = gbfgetpstr(file_in);; + wpt_tmp->icon_descr = gbfgetpstr(file_in); break; case 8: /* NULL Terminated (vs. pascal) descr */ wpt_tmp->notes = gbfgetcstr(file_in); break; - case 9: /* NULL Terminated (vs. pascal) link */ - link.url_ = gbfgetcstr(file_in); - break; - case 0x10: - link.url_link_text_ = gbfgetcstr(file_in); - break; + case 9: { /* NULL Terminated (vs. pascal) link */ + char* url = gbfgetcstr(file_in); + link.url_ = url; + if (url) { + xfree(url); + } + } + break; + case 0x10: { + char* ult = gbfgetcstr(file_in); + link.url_link_text_ = ult; + if (ult) { + xfree(ult); + } + } + break; case 0x63: wpt_tmp->latitude = gbfgetdbl(file_in); break; diff --git a/gpsbabel/garmin_gpi.cc b/gpsbabel/garmin_gpi.cc index 12dfde10a..dd83fdc6d 100644 --- a/gpsbabel/garmin_gpi.cc +++ b/gpsbabel/garmin_gpi.cc @@ -648,6 +648,9 @@ read_tag(const char* caller, const int tag, waypoint* wpt) } if ((mask & 0x10) && (str = gpi_read_string("Link"))) { waypt_add_url(wpt, str, str); + if (str) { + xfree(str); + } } break; diff --git a/gpsbabel/garmin_txt.cc b/gpsbabel/garmin_txt.cc index 882fba899..0a9064964 100644 --- a/gpsbabel/garmin_txt.cc +++ b/gpsbabel/garmin_txt.cc @@ -1200,8 +1200,7 @@ parse_waypoint(void) } break; case 17: { - UrlLink l(str); - wpt->AddUrlLink(l); + wpt->AddUrlLink(str); } break; case 18: diff --git a/gpsbabel/gdb.cc b/gpsbabel/gdb.cc index 561fe8ec5..f7b78ea42 100644 --- a/gpsbabel/gdb.cc +++ b/gpsbabel/gdb.cc @@ -652,13 +652,13 @@ read_waypoint(gt_waypt_classes_e* waypt_class_out) url_ct = FREAD_i32; for (i = url_ct; (i); i--) { - char* str = FREAD_CSTR; - if (str && *str) { + QString str = FREAD_CSTR_AS_QSTR; + if (!str.isEmpty()) { waypt_add_url(res, str, NULL); #if GDB_DEBUG DBG(GDB_DBG_WPTe, 1) printf(MYNAME "-wpt \"%s\" (%d): url(%d) = %s\n", - sn, wpt_class, url_ct - i, str); + sn, wpt_class, url_ct - i, CSTR(str)); #endif } } diff --git a/gpsbabel/mmo.cc b/gpsbabel/mmo.cc index 08bb085c3..ef8093e45 100644 --- a/gpsbabel/mmo.cc +++ b/gpsbabel/mmo.cc @@ -513,12 +513,11 @@ mmo_read_CObjWaypoint(mmo_data_t* data) cend = cx + strlen(cx); } - cx = lrtrim(xstrndup(cx, cend - cx)); - if (*cx) { - UrlLink l(cx); - wpt->AddUrlLink(l); - } else { - xfree(cx); + { + QString url = QString::fromUtf8(cx, cend-cx).trimmed(); + if (!url.isEmpty()) { + wpt->AddUrlLink(url); + } } if (*cend++) { diff --git a/gpsbabel/shape.cc b/gpsbabel/shape.cc index f5aa921b7..8aa9ba58e 100644 --- a/gpsbabel/shape.cc +++ b/gpsbabel/shape.cc @@ -187,8 +187,7 @@ my_read(void) wpt->longitude = shp->dfXMin; wpt->shortname = xstrdup(name); if (url) { - UrlLink l(url); - wpt->AddUrlLink(l); + wpt->AddUrlLink(url); } waypt_add(wpt); break; diff --git a/gpsbabel/tmpro.cc b/gpsbabel/tmpro.cc index f6f41e6b1..9f8eb02aa 100644 --- a/gpsbabel/tmpro.cc +++ b/gpsbabel/tmpro.cc @@ -143,8 +143,7 @@ data_read(void) */ holder = csv_stringtrim(s, "", 0); if (strstr(holder, "http:") != NULL) { - UrlLink link(holder); - wpt_tmp->AddUrlLink(link); + wpt_tmp->AddUrlLink(holder); } xfree(holder); break; diff --git a/gpsbabel/tpo.cc b/gpsbabel/tpo.cc index b317c3fd6..7c92604f5 100644 --- a/gpsbabel/tpo.cc +++ b/gpsbabel/tpo.cc @@ -1035,8 +1035,7 @@ void tpo_process_map_notes(void) notes[0] = '\0'; gbfread(notes, 1, name_length, tpo_file_in); notes[name_length] = '\0'; // Terminator - UrlLink l(notes); - waypoint_temp->AddUrlLink(l); + waypoint_temp->AddUrlLink(notes); //printf("Notes: %s\n", notes); xfree(notes); } @@ -1052,8 +1051,7 @@ void tpo_process_map_notes(void) notes[0] = '\0'; gbfread(notes, 1, name_length, tpo_file_in); notes[name_length] = '\0'; // Terminator - UrlLink l(notes); - waypoint_temp->AddUrlLink(l); + waypoint_temp->AddUrlLink(notes); //printf("Notes: %s\n", notes); xfree(notes); } diff --git a/gpsbabel/unicsv.cc b/gpsbabel/unicsv.cc index 38e83cd31..b6fca43e6 100644 --- a/gpsbabel/unicsv.cc +++ b/gpsbabel/unicsv.cc @@ -733,8 +733,7 @@ unicsv_parse_one_line(char* ibuf) break; case fld_url: { - UrlLink l(s); - wpt->AddUrlLink(l); + wpt->AddUrlLink(s); } break; -- 2.30.2